import Vue from 'vue'
import Router from 'vue-router'

// 用户管理
// import Users from './components/users.vue'
// import Login from './components/login'
// import Home from './components/home'
// import Welcome from './components/welcome'

// 权限管理
// import Rights from './components/rights.vue'
// import Roles from './components/roles.vue'

// 商品管理
// import Goods from './components/goods.vue'
// import Params from './components/params.vue'
// import Categories from './components/categories.vue'
// import Orders from './components/orders.vue'
// import Reports from './components/reports.vue'
// import AddGoods from './components/addGoods.vue'

/* 路由赖加载 */ 
const Users = () => import(/* webpackChunkName: 'users' */ './components/users.vue')
const Login = () => import(/* webpackChunkName: 'login' */ './components/login.vue')
const Home = () => import(/* webpackChunkName: 'home' */ './components/home.vue')
const Welcome = () => import(/* webpackChunkName: 'welcome' */ './components/welcome.vue')
const Rights = () => import(/* webpackChunkName: 'rights' */ './components/rights.vue')
const Roles = () => import(/* webpackChunkName: 'roles' */ './components/roles.vue')
const Goods = () => import(/* webpackChunkName: 'goods' */ './components/goods.vue')
const Params = () => import(/* webpackChunkName: 'params' */ './components/params.vue')
const Categories = () => import(/* webpackChunkName: 'categories' */ './components/categories.vue')
const Orders = () => import(/* webpackChunkName: 'orders' */ './components/orders.vue')
const Reports = () => import(/* webpackChunkName: 'reports' */ './components/reports.vue')
const AddGoods = () => import(/* webpackChunkName: 'addGoods' */ './components/addGoods.vue')

Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home', 
      component: Home, 
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: Users },
        { path: '/roles', component: Roles },
        { path: '/rights', component: Rights },
        { path: '/goods', component: Goods },
        { path: '/addGoods', component: AddGoods },
        { path: '/params', component: Params },
        { path: '/categories', component: Categories },
        { path: '/orders', component: Orders },
        { path: '/reports', component: Reports }
      ]
    }
  ]
})

//  路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()

  // 获取 token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')

  next()
})

export default router
